package com.magist.manage.system.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.DateUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.magist.manage.system.entity.OffNews;
import com.magist.manage.system.dto.OffNewsDto;
import com.magist.manage.system.service.OffNewsService;

@Api(value = "RestOffNewsController", description = "RestOffNewsController接口")
@RestController
@RequestMapping("/rest/offNews")
public class RestOffNewsController extends BaseController {

	@Autowired
	private OffNewsService offNewsService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("offNewsDto") OffNewsDto offNewsDto) {
		OffNews offNews = new OffNews();
		BeanUtils.copyProperties(offNewsDto,offNews);
		// 时间转换
		offNews.setNewsDate(DateUtil.parse(offNewsDto.getNewsDate(),DateUtil.yyyy_MM_dd));
		offNews.setId(offNewsService.getNewId());
		offNewsService.insert(offNews);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("offNewsDto") OffNewsDto offNewsDto) {
		OffNews oldOffNews = offNewsService.getById(offNewsDto.getId());
		if(oldOffNews==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		OffNews offNews = new OffNews();
		BeanUtils.copyProperties(offNewsDto,offNews);
		// 时间转换
		offNews.setNewsDate(DateUtil.parse(offNewsDto.getNewsDate(),DateUtil.yyyy_MM_dd));
		offNews.setModifyTime(oldOffNews.getModifyTime());
		if (offNewsService.update(offNews)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("offNewsDto") OffNewsDto offNewsDto) {
		ResultMessage result = ResultMessage.success();
		OffNews offNews = new OffNews();
		BeanUtils.copyProperties(offNewsDto,offNews);
		String newsName = offNews.getNewsName();
		String newsCode = offNews.getNewsCode();
		if(!StringUtils.isEmpty(newsName)) {
			offNews.setNewsName("%"+newsName+ "%");
		}
		if(!StringUtils.isEmpty(newsCode)) {
			offNews.setNewsCode("%"+newsCode+ "%");
		}
		PageInfo<OffNews> pageInfo = offNewsService.page(offNews,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		OffNews offNews = offNewsService.getById(id);
		if(offNews==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(offNewsService.delete(offNews)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		OffNews offNews = offNewsService.getById(id);
		if(offNews==null)
			throw new BusinessException("无效的数据!");
		OffNewsDto offNewsDto = new OffNewsDto();
		BeanUtils.copyProperties(offNews,offNewsDto);
		// 时间转换
		offNewsDto.setNewsDate(DateUtil.format(offNews.getNewsDate(),DateUtil.yyyy_MM_dd));
		ResultMessage result = ResultMessage.success();
		result.setContent(offNewsDto);
		return result;
	}
}
